#!/bin/bash

# Slackware build script for rpcs3

# Copyright 2022 Steven Voges <Oregon, USA>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=rpcs3
VERSION=${VERSION:-0.0.24}
ASMJIT=${ASMJIT:-06d0badec53710a4f572cf5642881ce570c5d274}
CUBEB=${CUBEB:-dc511c6b3597b6384d28949285b9289e009830ea}
FLATBUFFERS=${FLATBUFFERS:-615616cb5549a34bdf288c04bc1b94bd7a65c396}
GLSLANG=${GLSLANG:-adbf0d3106b26daa237b10b9bf72b1af7c31092d}
HIDAPI=${HIDAPI:-c2aa9dd37c7b401b918fd56e18a3bac7f8f00ec2}
ITTAPI=${ITTAPI:-3.18.12}
LLVM=${LLVM:-5521155be5c869b0b760e1dec86c41cdbb7a75c0}
SOUNDTOUCH=${SOUNDTOUCH:-83cfba67b6af80bb9bfafc0b324718c4841f2991}
SPIRVHEADERS=${SPIRVHEADERS:-4995a2f2723c401eb0ea3e10c81298906bf1422b}
SPIRVTOOLS=${SPIRVTOOLS:-7826e1941eab1aa66fbe84c48b95921bff402a96}
WOLFSSL=${WOLFSSL:-57aac1c50b45275c7a99eca32ad985998b292dc8}
YAMLCPP=${YAMLCPP:-0b67821f307e8c6bf0eba9b6d3250e3cf1441450}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}
DEPS=$TMP/$PRGNAM-$VERSION/3rdparty

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION

mkdir -p $TMP/$PRGNAM-$VERSION/build/3rdparty/llvm_build/ittapi
tar xvf $CWD/SPIRV-Headers-$SPIRVHEADERS.tar.gz -C \
  $DEPS/SPIRV/SPIRV-Headers --strip-components 1
tar xvf $CWD/SPIRV-Tools-$SPIRVTOOLS.tar.gz -C \
  $DEPS/SPIRV/SPIRV-Tools --strip-components 1
tar xvf $CWD/asmjit-$ASMJIT.tar.gz -C \
  $DEPS/asmjit/asmjit --strip-components 1
tar xvf $CWD/cubeb-$CUBEB.tar.gz -C \
  $DEPS/cubeb/cubeb --strip-components 1
tar xvf $CWD/flatbuffers-$FLATBUFFERS.tar.gz -C \
  $DEPS/flatbuffers --strip-components 1
tar xvf $CWD/glslang-$GLSLANG.tar.gz -C \
  $DEPS/glslang/glslang --strip-components 1
tar xvf $CWD/hidapi-$HIDAPI.tar.gz -C \
  $DEPS/hidapi/hidapi --strip-components 1
tar xvf $CWD/ittapi-$ITTAPI.tar.gz -C \
  $TMP/$PRGNAM-$VERSION/build/3rdparty/llvm_build/ittapi --strip-components 1
tar xvf $CWD/llvm-mirror-$LLVM.tar.gz -C \
  $DEPS/../llvm --strip-components 1
tar xvf $CWD/soundtouch-$SOUNDTOUCH.tar.gz -C \
  $DEPS/SoundTouch/soundtouch --strip-components 1
tar xvf $CWD/wolfssl-$WOLFSSL.tar.gz -C \
  $DEPS/wolfssl/wolfssl --strip-components 1
tar xvf $CWD/yaml-cpp-$YAMLCPP.tar.gz -C \
  $DEPS/yaml-cpp/yaml-cpp --strip-components 1

#Bypass git checkout check for ittapi
sed -i 's/if(NOT GIT_CHECKOUT_RESULT EQUAL "0")/if(GIT_CHECKOUT_RESULT EQUAL "0")/g' \
  llvm/lib/ExecutionEngine/IntelJITEvents/CMakeLists.txt

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

cd build
  cmake \
    -Wno-dev \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DUSE_SYSTEM_CURL=ON \
    -DUSE_SYSTEM_FAUDIO=ON \
    -DUSE_SYSTEM_FFMPEG=ON \
    -DUSE_SYSTEM_FLATBUFFERS=OFF \
    -DUSE_SYSTEM_LIBPNG=ON \
    -DUSE_SYSTEM_LIBUSB=ON \
    -DUSE_SYSTEM_PUGIXML=ON \
    -DUSE_SYSTEM_XXHASH=ON \
    -DUSE_SYSTEM_ZLIB=ON \
    -DCMAKE_BUILD_TYPE=Release ..
  make
  make install/strip DESTDIR=$PKG
cd ..

rm -r $PKG/usr/share/$PRGNAM/git

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  LICENSE README.md \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
